// +----------------------------------------------------------------------
// | JavaWeb_EleVue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2021 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <javaweb520@gmail.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.entity.Dict;
import com.javaweb.system.entity.DictData;
import com.javaweb.system.mapper.DictDataMapper;
import com.javaweb.system.mapper.DictMapper;
import com.javaweb.system.query.DictDataQuery;
import com.javaweb.system.service.IDictDataService;
import com.javaweb.system.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 字典项管理表 服务实现类
 * </p>
 *
 * @author 鲲鹏
 * @since 2020-11-01
 */
@Service
public class DictDataServiceImpl extends BaseServiceImpl<DictDataMapper, DictData> implements IDictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private DictMapper dictMapper;

    private HashMap<String, DictData> formCache;

    private Date formCacheTs;

    private long min30 = 1800000;

    private long cacheTime = 0;
    /**
     * 获取字典项列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        DictDataQuery dictQuery = (DictDataQuery) query;
        // 查询条件
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        // 字典ID
        if (StringUtils.isNotNull(dictQuery.getDictId())) {
            queryWrapper.eq("dict_id", dictQuery.getDictId());
        }
        // 字典项名称
        if (!StringUtils.isEmpty(dictQuery.getName())) {

            queryWrapper.in("name", dictQuery.getName());
        }
        // 字典项编码
        if (!StringUtils.isEmpty(dictQuery.getCode())) {
            queryWrapper.like("code", dictQuery.getCode());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");

        // 查询分页数据
        IPage<DictData> page = new Page<>(dictQuery.getPage(), dictQuery.getLimit());
        IPage<DictData> pageData = dictDataMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public String getNCUserId(String dataType) {
        return dictDataMapper.getNCUserId("KDL001",dataType);
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public String[] getBillType(String dictCode) {
        String[] result= dictDataMapper.getBillType(dictCode);
        return result;
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public String getDicStatus(String dicCode, String dicDataCode) {
        return dictDataMapper.getDicStatus(dicCode,dicDataCode);
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public String getCodeByName(String dicCode, String name) {
        return dictDataMapper.getCodeByName(dicCode,name);
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public HashMap<String, DictData> getFormCache(String dicCode) {
        long curTime = System.currentTimeMillis();
        if (formCache != null && curTime - cacheTime < min30) {
            return formCache;
        }
        cacheTime = curTime;
        Dict dict = dictMapper.selectOne(new QueryWrapper<Dict>().eq("code", dicCode));
        HashMap<String, DictData> newCache = new HashMap<>();
        List<DictData> dictDataList = dictDataMapper.selectList(new QueryWrapper<DictData>().eq("dict_id", dict.getId()));
        if (!CollectionUtils.isEmpty(dictDataList)) {
            for (DictData dictData : dictDataList) {
                newCache.put(dictData.getDictType() + dictData.getName(), dictData);
            }
        }
        formCache = newCache;
        return formCache;
    }

    /**
     * 添加或编辑
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult edit(DictData entity) {
        if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
            entity.setUpdateUser(ShiroUtils.getUserId());
        } else {
            entity.setCreateUser(ShiroUtils.getUserId());
        }
        return super.edit(entity);
    }

    @Override
    @SpecifyDataSource(DataSourceType.SINESYS)
    public JsonResult deleteByIds(Integer[] ids) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        dictDataMapper.delete(queryWrapper);
        return JsonResult.success();
    }
}
